package com.ruoyi.web.controller.diagnosis;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.diagnosis.domain.Device;
import com.ruoyi.diagnosis.domain.PatientInfo;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.diagnosis.domain.Result;
import com.ruoyi.diagnosis.service.IResultService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 扫描结果Controller
 * 
 * @author fansea
 * @date 2024-07-15
 */
@RestController
@RequestMapping("/diagnosis/scan")
public class ResultController extends BaseController
{
    @Autowired
    private IResultService resultService;


    /**
     * 查询扫描结果列表
     */
    @PreAuthorize("@ss.hasPermi('diagnosis:scan:list')")
    @GetMapping("/list")
    public TableDataInfo list(Result result)
    {
        startPage();
        List<Result> list = resultService.selectResultList(result);
        return getDataTable(list);
    }

    /**
     * 导出扫描结果列表
     */
    @PreAuthorize("@ss.hasPermi('diagnosis:scan:export')")
    @Log(title = "扫描结果", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Result result)
    {
        List<Result> list = resultService.selectResultList(result);
        ExcelUtil<Result> util = new ExcelUtil<Result>(Result.class);
        util.exportExcel(response, list, "扫描结果数据");
    }

    /**
     * 获取扫描结果详细信息
     */
    @PreAuthorize("@ss.hasPermi('diagnosis:scan:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(resultService.selectResultById(id));
    }

    /**
     * 新增扫描结果
     */
    @PreAuthorize("@ss.hasPermi('diagnosis:scan:add')")
    @Log(title = "扫描结果", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Result result)
    {
        return toAjax(resultService.insertResult(result));
    }

    /**
     * 修改扫描结果
     */
    @PreAuthorize("@ss.hasPermi('diagnosis:scan:edit')")
    @Log(title = "扫描结果", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Result result)
    {
        return toAjax(resultService.updateResult(result));
    }

    /**
     * 删除扫描结果
     */
    @PreAuthorize("@ss.hasPermi('diagnosis:scan:remove')")
    @Log(title = "扫描结果", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(resultService.deleteResultByIds(ids));
    }

    /**
     * 开启串口连接设备
     */
    @PostMapping("/openSerialPort")
    public AjaxResult openSerialPort(@RequestBody Device device)
    {
        /*if (resultService.openSerialPort(device)) {
            return AjaxResult.success("串口连接成功");
        }
        return AjaxResult.error("串口连接失败");*/
        String portName = resultService.openSerialPort(device);
        if (portName == null){
            return AjaxResult.error("串口打开错误");
        }
        return AjaxResult.success("串口："+portName+"打开成功");
    }

    @GetMapping("/infoEnter")
    public AjaxResult infoEnter(PatientInfo patientInfo)
    {
        resultService.infoEnter(patientInfo);
        return AjaxResult.success();
    }
    @GetMapping("/runScanner")
    public AjaxResult runScanner(String portName)
    {
        return AjaxResult.success();
    }










}
